﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace com.sharepointutils.application
{
    public partial class OpenSiteForm : Form
    {
        public String Site { get; set; }
        public String Username { get; set; }
        public String Password { get; set; }
        public String Domain { get; set; }
        private List<String> sites = new List<string>();

        public OpenSiteForm()
        {
            InitializeComponent();
        }

        private void btn_connect_Click(object sender, EventArgs e)
        {
            Site = combo_site.Text;
            Username = txt_username.Text;
            Password = txt_password.Text;
            Domain = txt_domain.Text;
            saveSitesToSettings();
            saveUserSettings();
            Properties.Settings.Default.Save();
            this.Close();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void OpenSiteForm_Load(object sender, EventArgs e)
        {
            txt_username.Text = Properties.Settings.Default.Username;
            txt_password.Text = Properties.Settings.Default.Password;
            txt_domain.Text = Properties.Settings.Default.Domain;
            loadSitesFromSettings();
        }

        private void saveUserSettings()
        {
            Properties.Settings.Default.Username  = txt_username.Text;
            Properties.Settings.Default.Password = txt_password.Text;
            Properties.Settings.Default.Domain = txt_domain.Text;
        }

        private void saveSitesToSettings()
        {
            if (!sites.Contains(combo_site.Text))
            {
                sites.Add(combo_site.Text);
                String saveSettings = "";
                foreach(String s in sites)
                {
                    saveSettings += s + ";"; 
                }
                Properties.Settings.Default.Sites = saveSettings;
            }
        }

        private void loadSitesFromSettings()
        {
            String[] temp = Properties.Settings.Default.Sites.Split(';');
            foreach (String s in temp)
            {
                if (!String.IsNullOrEmpty(s))
                {
                    sites.Add(s);
                }
            }
            combo_site.DataSource = sites;
        }
    }
}
